package com.mt.api.modules.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Splitter;
import com.mt.common.response.WebResponse;
import com.mt.common.response.WebResponseUtils;
import com.mt.common.utils.PloverDateUtil;
import com.mt.api.constants.constant.Constants;
import com.mt.api.modules.domain.AppCargo;
import com.mt.api.modules.domain.AppSku;
import com.mt.api.modules.domain.bo.AppCargoBo;
import com.mt.api.modules.domain.bo.AppCargoEditBo;
import com.mt.api.modules.domain.bo.CargoPageBo;
import com.mt.api.modules.domain.bo.CargoReportBo;
import com.mt.api.modules.domain.dto.CargoSalesReportDto;
import com.mt.api.modules.domain.dto.StockStatQtyByCargoDto;
import com.mt.api.modules.domain.vo.CargoSalesReportVo;
import com.mt.api.modules.domain.vo.CargoSimpleVo;
import com.mt.api.modules.manager.ICargoManager;
import com.mt.api.modules.manager.IOrderDetailManager;
import com.mt.api.modules.manager.ISkuManager;
import com.mt.api.modules.manager.IStockManager;
import com.mt.api.modules.mapper.AppCargoMapper;
import com.mt.api.modules.mapstruct.CargoMapStruct;
import com.mt.api.modules.service.AppCargoService;
import com.mt.api.util.*;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author plover
 * @description 服务实现
 * @date 2020-07-06
 **/
@Service
@RequiredArgsConstructor
public class AppCargoServiceImpl extends ServiceImpl<AppCargoMapper, AppCargo> implements AppCargoService {

    private final ICargoManager cargoManager;
    private final ISkuManager skuManager;
    private final IStockManager stockManager;
    private final IOrderDetailManager orderDetailManager;

    private final CargoMapStruct cargoMapStruct;


    @Override
    public WebResponse editCargo(AppCargoEditBo bo) {
        if (Objects.isNull(bo.getId())) {
            AppCargo cargo = cargoManager.getCargoByCargoNo(bo.getCargoNo());
            if (Objects.nonNull(cargo)) {
                return WebResponseUtils.error("商品编码已存在");
            }
        }
        AppCargo cargo = new AppCargo();
        cargo.setId(bo.getId());
        cargo.setCargoNo(bo.getCargoNo());
        cargo.setCargoName(bo.getCargoName());
        cargo.setRemark(bo.getRemark());
        cargo.setPicUrl(bo.getPicUrl());
        cargo.setPurchasePrice(AmountUtil.multiplyHundred(bo.getPurchasePrice()));
        cargo.setRetailPrice(AmountUtil.multiplyHundred(bo.getRetailPrice()));
        cargo.setWholesalePrice(AmountUtil.multiplyHundred(bo.getWholesalePrice()));
        cargo.setStatus(Constants.SaleState.ON);
        cargoManager.saveOrUpdate(cargo);

        List<String> colorList;
        List<String> sizeList;
        String colors = bo.getColors();
        String sizes = bo.getSizes();
        if (StringUtils.isNotEmpty(colors)) {
            colorList = Splitter.on(",").splitToList(colors);
        } else {
            colorList = Arrays.asList("均色");
        }
        if (StringUtils.isNotEmpty(sizes)) {
            sizeList = Splitter.on(",").splitToList(sizes);
        } else {
            sizeList = Arrays.asList("均码");
        }

        for (String color : colorList) {
            for (String size : sizeList) {
                skuManager.saveSkuIfNotExist(cargo.getId(), cargo.getCargoNo(), color, size, bo.getRetailPrice(), bo.getWholesalePrice());
            }
        }
        return WebResponseUtils.success(cargo.getId());
    }

    @Override
    public WebResponse cargoOn(AppCargoBo appCargoBo) {
        AppCargo cargo = new AppCargo();
        cargo.setId(appCargoBo.getId());
        cargo.setStatus(Constants.SaleState.ON);
        cargoManager.updateById(cargo);
        return WebResponseUtils.success();
    }

    @Override
    public WebResponse cargoOff(AppCargoBo appCargoBo) {
        AppCargo cargo = new AppCargo();
        cargo.setId(appCargoBo.getId());
        cargo.setStatus(Constants.SaleState.OFF);
        cargoManager.updateById(cargo);
        return WebResponseUtils.success();
    }

    @Override
    public WebResponse<CargoSimpleVo> getByCargoNo(String cargoNo) {
        AppCargo cargo = cargoManager.getCargoByCargoNo(cargoNo);
        if (Objects.isNull(cargo)) {
            return WebResponseUtils.errorParam("款号不存在或者已下架");
        }
        CargoSimpleVo cargoSimpleVo = cargoMapStruct.toVo(cargo);
        return WebResponseUtils.success(cargoSimpleVo);
    }

    @Override
    public WebResponse<CargoSimpleVo> getByCargoId(Integer cargoId) {
        AppCargo cargo = cargoManager.getById(cargoId);
        if (Objects.isNull(cargo)) {
            return WebResponseUtils.errorParam("款号不存在");
        }
        CargoSimpleVo cargoSimpleVo = cargoMapStruct.toVo(cargo);

        List<AppSku> skuList = skuManager.getListByCargoId(cargoId);
        List<String> colors = skuList.stream().map(AppSku::getColor).distinct().collect(Collectors.toList());
        List<String> sizes = skuList.stream().map(AppSku::getSize).distinct().collect(Collectors.toList());
        cargoSimpleVo.setColorList(colors);
        cargoSimpleVo.setSizeList(sizes);

        //统计商品销量
        int salesQty = orderDetailManager.getSalesQtyByCargoId(cargoId);
        cargoSimpleVo.setSalesQty(salesQty);

        List<StockStatQtyByCargoDto> stockStatQtyList = stockManager.statQtyByCargo(Arrays.asList(cargoId));
        if (CollectionUtils.isNotEmpty(stockStatQtyList)) {
            StockStatQtyByCargoDto stockStatQtyByCargoDto = stockStatQtyList.get(0);
            cargoSimpleVo.setStoreQty(stockStatQtyByCargoDto.getQty());
            cargoSimpleVo.setStorePurchasePrice(AmountUtil.divideHundred(stockStatQtyByCargoDto.getQty() * cargo.getPurchasePrice()));
        }

        cargoSimpleVo.setOrderGrossProfitAmount(AmountUtil.divideHundred(cargoSimpleVo.getSalesQty() * (cargo.getWholesalePrice() - cargo.getPurchasePrice())));
        return WebResponseUtils.success(cargoSimpleVo);
    }

    @Override
    public WebResponse getCargoPage(CargoPageBo bo) {
        Page page = cargoManager.getCargoPage(bo);
        List<AppCargo> cargoList = page.getRecords();
        List<CargoSimpleVo> list = cargoList.stream().map(cargo -> cargoMapStruct.toVo(cargo)).collect(Collectors.toList());
        page.setRecords(list);
        return WebResponseUtils.successPage(page);
    }

    @Override
    public WebResponse<CargoSalesReportVo> salesReport(CargoReportBo bo) {
        List<CargoSalesReportDto> salesReportDtoList = cargoManager.getSalesDayStat(bo);

        CargoSalesReportVo vo = new CargoSalesReportVo();
        salesReportDtoList = salesReportDtoList.stream().sorted(Comparator.comparing(CargoSalesReportDto::getOrderDate))
                .collect(Collectors.toList());
        List<String> xAxisData = salesReportDtoList.stream().map(cargoSalesReportDto -> PloverDateUtil.dateToMMdd(cargoSalesReportDto.getOrderDate())).collect(Collectors.toList());
        List<Integer> seriesData = salesReportDtoList.stream().map(CargoSalesReportDto::getQty).collect(Collectors.toList());
        vo.setXAxisData(xAxisData);
        vo.setSeriesData(seriesData);
        return WebResponseUtils.success(vo);
    }
}